package com.mrogrape.notepad.ui.filechooser;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;

import com.mrogrape.notepad.data.SystemContext;
import com.mrogrape.system.Environment;
import com.mrogrape.util.StringUtils;


public class SaveOptionPane extends JPanel {

	/** serialVersionUID */
	private static final long serialVersionUID = 1L;


	public SaveOptionPane(String currentCharset){

		if(StringUtils.isEmpty(currentCharset)){
			this.currentCharset = System.getProperty("file.encoding");
		}
		else{
			this.currentCharset = currentCharset;
		}

		this.initializeComponent();
		this.prepare();
	}

	/**
	 * 获取当前编码
	 * @return
	 */
	public String getCharset(){

		if(this.currentCharsetRadio.isSelected()){
			return this.currentCharset;
		}
		else if(this.systemDefaultRadio.isSelected()){
			return this.systemCharsetView.getText();
		}
		else{
			return this.charsetSelector.getSelectedItem().toString();
		}
	}

	/**
	 * 设置当前编码
	 * @param option
	 */
	public void setCharset(String charset){

		if(StringUtils.isEmpty(charset)){
			this.currentCharset = System.getProperty("file.encoding");
		}
		else{
			this.currentCharset = charset;
		}
	}
	
	/**
	 * 设置默认选中的用户字符集。
	 * 
	 * @param ls
	 */
	public void setSelectedLineSeparator(String ls){
		if(SystemContext.getSystemLineSeparator().equals(ls)){
			this.systemLineSeparatorRadio.setSelected(true);
		}
		else if("\n".equals(ls)){
			this.unixRadio.setSelected(true);
		}
		else if("\r\n".equals(ls)){
			this.windowsRadio.setSelected(true);
		}
	}
	
	/**
	 * 获取用户选择的字符集
	 * @return
	 */
	public String getSelectedLineSeparator(){
		if(this.systemLineSeparatorRadio.isSelected()){
			return SystemContext.getSystemLineSeparator();
		}
		else if(this.unixRadio.isSelected()){
			return "\n";
		}
		else{
			return "\r\n";
		}
	}

	private void initializeComponent(){
		this.setPreferredSize(new Dimension(200, 269));

		JPanel charsetPanel = new JPanel();
		charsetPanel.setBorder(new TitledBorder(null, "Charset", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		this.panel = new JPanel();
		this.panel.setBorder(new TitledBorder(null, "Line Separator", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(charsetPanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
						.addComponent(this.panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(charsetPanel, GroupLayout.PREFERRED_SIZE, 152, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(this.panel, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		
		this.systemLineSeparatorRadio = new JRadioButton("System Default");
		this.systemLineSeparatorRadio.setSelected(true);
		this.lineSeparatorGroup.add(this.systemLineSeparatorRadio);
		
		this.unixRadio = new JRadioButton("UNIX");
		this.lineSeparatorGroup.add(this.unixRadio);
		
		this.windowsRadio = new JRadioButton("Windows");
		this.lineSeparatorGroup.add(this.windowsRadio);
		GroupLayout gl_panel = new GroupLayout(this.panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(this.systemLineSeparatorRadio)
						.addComponent(this.unixRadio)
						.addComponent(this.windowsRadio))
					.addContainerGap(43, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(this.systemLineSeparatorRadio)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(this.unixRadio)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(this.windowsRadio)
					.addContainerGap(60, Short.MAX_VALUE))
		);
		this.panel.setLayout(gl_panel);

		this.currentCharsetRadio = new JRadioButton("Current Charset");
		this.currentCharsetRadio.setSelected(true);
		this.currentCharsetRadio.addActionListener(new RaidoChangeListener());
		this.charsetBtnGroup.add(this.currentCharsetRadio);

		this.systemDefaultRadio = new JRadioButton("System Default");
		this.systemDefaultRadio.addActionListener(new RaidoChangeListener());
		this.charsetBtnGroup.add(this.systemDefaultRadio);

		this.specifiedRadio = new JRadioButton("Specified");
		this.specifiedRadio.addActionListener(new RaidoChangeListener());
		this.charsetBtnGroup.add(this.specifiedRadio);

		this.charsetSelector = new JComboBox();

		this.systemCharsetView = new JLabel(System.getProperty("file.encoding"));
		this.systemCharsetView.setForeground(Color.GRAY);

		this.currentCharsetView = new JLabel(this.currentCharset);
		GroupLayout gl_charsetPanel = new GroupLayout(charsetPanel);
		gl_charsetPanel.setHorizontalGroup(
			gl_charsetPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_charsetPanel.createSequentialGroup()
					.addGroup(gl_charsetPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_charsetPanel.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_charsetPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_charsetPanel.createSequentialGroup()
									.addGap(21)
									.addComponent(this.currentCharsetView))
								.addComponent(this.currentCharsetRadio)
								.addGroup(gl_charsetPanel.createSequentialGroup()
									.addGap(21)
									.addComponent(this.systemCharsetView))
								.addComponent(this.specifiedRadio)
								.addComponent(this.systemDefaultRadio)))
						.addGroup(Alignment.TRAILING, gl_charsetPanel.createSequentialGroup()
							.addGap(29)
							.addComponent(this.charsetSelector, 0, 123, Short.MAX_VALUE)))
					.addContainerGap())
		);
		gl_charsetPanel.setVerticalGroup(
			gl_charsetPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_charsetPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(this.currentCharsetRadio)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(this.currentCharsetView)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(this.systemDefaultRadio)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(this.systemCharsetView)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(this.specifiedRadio)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(this.charsetSelector, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(15, Short.MAX_VALUE))
		);
		charsetPanel.setLayout(gl_charsetPanel);
		this.setLayout(groupLayout);
	}

	private void prepare(){

		this.charsetSelector.setEnabled(this.specifiedRadio.isSelected());
	}




	/** 选项 */
	private String currentCharset = null;
	private String[] charsets = null;

	private JRadioButton currentCharsetRadio;
	private JRadioButton specifiedRadio;
	private JComboBox charsetSelector;
	private JLabel systemCharsetView;
	private JRadioButton systemDefaultRadio;
	private final ButtonGroup charsetBtnGroup = new ButtonGroup();
	private JLabel currentCharsetView;
	private JPanel panel;
	private JRadioButton systemLineSeparatorRadio;
	private JRadioButton unixRadio;
	private JRadioButton windowsRadio;
	private final ButtonGroup lineSeparatorGroup = new ButtonGroup();

	private class RaidoChangeListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {

			// 用户是否选择指定编码
			boolean specifiedRadioSelected = SaveOptionPane.this.specifiedRadio.isSelected();

			// 设置编码选择器是否可用
			SaveOptionPane.this.charsetSelector.setEnabled(specifiedRadioSelected);

			if(specifiedRadioSelected && SaveOptionPane.this.charsets == null){

				SaveOptionPane.this.charsets = Environment.availableCharsets();
				SaveOptionPane.this.charsetSelector.setModel(new DefaultComboBoxModel(SaveOptionPane.this.charsets));
				SaveOptionPane.this.charsetSelector.setSelectedItem(SaveOptionPane.this.currentCharsetView.getText());
			}

		}

	}

	public static void main(String args[]){
		JFrame f = new JFrame("test");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.getContentPane().add(new SaveOptionPane("GB2312"));
		f.setVisible(true);
		f.pack();
	}
}
